package com.zp.business.controller.bedRec;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.business.entity.bedRec.bo.BedRecBo;
import com.zp.business.entity.bedRec.vo.BedRecVo;
import com.zp.business.service.bedRec.IBedRecService;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.core.controller.BaseController;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.zp.common.myBatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 床位控制器
 * 前端访问路由地址为:/business/bedRec
 *
 * @author zhang peng
 * @ date 2024-06-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/bedRec")
public class BedRecController extends BaseController {

    private final IBedRecService iBedRecService;

    /**
     * 查询床位列表 (分页)
     */
    @SaCheckPermission("business:bedRec:list")
    @GetMapping("/selectPage")
    public TableDataInfo<BedRecVo> selectPage(BedRecBo bo, PageQuery pageQuery) {
        return iBedRecService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询床位列表 (不分页)
     */
    @SaCheckPermission("business:bedRec:list")
    @GetMapping("/list")
    public R<List<BedRecVo>> list(BedRecBo bo) {
        List<BedRecVo> list = iBedRecService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出床位列表
     */
//    @SaCheckPermission("business:bedRec:export")
    @Log(title = "床位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BedRecBo bo, HttpServletResponse response) {
        List<BedRecVo> list = iBedRecService.queryList(bo);
        ExcelUtil.exportExcel(list, "床位", BedRecVo.class, response);
    }

    /**
     * 获取床位详细信息
     *
     * @param bedId 主键
     */
//    @SaCheckPermission("business:bedRec:list")
    @GetMapping("/{bedId}")
    public R<BedRecVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String bedId) {
        return R.ok(iBedRecService.queryById(bedId));
    }

    /**
     * 获取护理单元床位对照
     *
     * @param wordCode 护理单元
     */
    @GetMapping("/getDeptVsBedList/{wordCode}/{orgId}")
    public R<List<BedRecVo>> getDeptVsBedList(@NotNull(message = "护理单元不能为空") @PathVariable String wordCode,
                                              @NotNull(message = "机构id不能为空") @PathVariable String orgId) {
        return R.ok(iBedRecService.getDeptVsBedList(wordCode, orgId));
    }

    /**
     * 新增床位
     */
//    @SaCheckPermission("business:bedRec:add")
    @Log(title = "床位", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BedRecBo bo) {
        return toAjax(iBedRecService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改床位
     */
//    @SaCheckPermission("business:bedRec:edit")
    @Log(title = "床位", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BedRecBo bo) {
        return toAjax(iBedRecService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除床位
     *
     * @param bedIds 主键串
     */
//    @SaCheckPermission("business:bedRec:remove")
    @Log(title = "床位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bedIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] bedIds) {
        return toAjax(iBedRecService.deleteWithValidByIds(Arrays.asList(bedIds), true) ? 1 : 0);
    }
}
